<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订购人" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订购人证件号码" prop="orderId" label-width="110px">
        <el-input
          v-model="queryParams.ConsigneeCode"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订单时间" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="清单状态" prop="push_inventory">
        <el-select v-model="queryParams.pushInventory" placeholder="清单状态" filterable allow-create clearable size="small">
          <el-option
            v-for="(dict,index) in PushInventoryOptions"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          :disabled="multiple"
          @click="declare()"
        >批量申报</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="select()"
        >批量同步</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-male"
          size="mini"
          :disabled="multiple"
          @click="exportXml()"
        >导出XML报文</el-button>
      </el-col>
      <el-col :span="1.5" style="float: right">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          :disabled="multiple"
          @click="exportOrder"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5" style="float: right">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          style="float: right"
          size="mini"
          :disabled="multiple"
          @click="exportOrderGoods"
        >导出商品
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订单日期" align="center" prop="orderDate" width="150" />
      <el-table-column label="订购人" align="center" prop="buyerName" />
      <!--      <el-table-column label="交易流水号" align="center" prop="payOrderNo" :show-overflow-tooltip="true" />-->
      <el-table-column label="订购人证件号" align="center" prop="ConsigneeCode" :show-overflow-tooltip="true" />
      <el-table-column label="实付款" align="center" prop="realPayment" />
      <el-table-column label="清单状态" align="center" :formatter="statusFormat" />
      <el-table-column label="回执信息" align="center" prop="clear_returnInfo" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="详情" align="center" width="50">
        <template v-slot="scope">
          <span style="color: #20a0ff;cursor:pointer" @click="see(scope.row)">查看</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template v-slot="scope">
          <el-button
            :disabled="scope.row.push_inventory === 800 || scope.row.push_inventory === 899"
            size="mini"
            type="primary"
            @click="declare(scope.row,'申报')"
          >申报/变更</el-button>
          <!--          <el-button-->
          <!--            v-if="scope.row.invt_no !== null && scope.row.invt_no !== ''"-->
          <!--            size="mini"-->
          <!--            type="success"-->
          <!--            @click="declare(scope.row,'变更')"-->
          <!--          >变更申报</el-button>-->
          <el-button
            size="mini"
            type="success"
            :disabled="scope.row.push_inventory === 0 ? true : false"
            @click="select(scope.row)"
          >同步回执</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!--    回执信息-->
    <el-dialog title="回执信息" :visible.sync="dialogVisible" width="30%">
      <span>{{ info }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <!-- 查看详情 -->
    <ClearOrderDetails :show.sync="show" :form="form" />
  </div>
</template>
<script>
import {
  clearList, exportClearOrder, exportClearOrderGoods,
  exportInventoryXml,
  orderClearPush,
  orderClearSelect,
  orderInfo
} from '@/api/order'
export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 非多个禁用
      multiple: true,
      // 是否显示弹出层
      dialogVisible: false,
      info: '',
      // 订单状态
      PushInventoryOptions: [
        { 'value': 0, 'label': '未推送' },
        { 'value': 1, 'label': '电子口岸已暂存' },
        { 'value': 2, 'label': '电子口岸申报中' },
        { 'value': 3, 'label': '发送海关成功' },
        { 'value': 4, 'label': '发送海关失败' },
        { 'value': 100, 'label': '海关退单' },
        { 'value': 120, 'label': '海关入库' },
        { 'value': 300, 'label': '人工审核' },
        { 'value': 399, 'label': '海关审结' },
        { 'value': 800, 'label': '放行' },
        { 'value': 899, 'label': '结关' },
        { 'value': 500, 'label': '查验' },
        { 'value': 501, 'label': '扣留移送通关' },
        { 'value': 502, 'label': '扣留移送缉私' },
        { 'value': 503, 'label': '扣留移送法规' },
        { 'value': 599, 'label': '其它扣留' },
        { 'value': 700, 'label': '退运' }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        orderId: undefined,
        ConsigneeCode: undefined,
        buyerName: undefined,
        // 日期范围
        orderDate: [],
        pushInventory: undefined
      },
      form: {},
      show: false,
      ids: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单详情 */
    see(row) {
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 查询清单列表 */
    getList() {
      this.loading = true
      // console.log(this.queryParams)
      clearList(this.queryParams).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.push_inventory
      for (const d of this.PushInventoryOptions) {
        if (row.push_inventory === d.value) {
          status = d.label
          break
        }
      }
      return status
    },
    /** (申报/变更/重新)清单 */
    declare(row, type = '申报') {
      const that = this
      const postIds = row ? row.orderId : this.ids.toString()
      that.$confirm('是否' + type + postIds + '等清单信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        orderClearPush({ orderId: postIds }).then(response => {
          // console.log(response)
          if (response.code === 10000) {
            that.$message.success(response.message)
          }
          that.getList()
        })
      }).catch(() => {})
    },
    /** 导出xml文件 */
    exportXml() {
      exportInventoryXml({ orderId: this.ids }).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          const url = window.URL.createObjectURL(
            new Blob([response.data], { type: 'text/xml' })
          )
          // 这里会生成一个url   blob:http://www.ceshi.com:111/5a8d3b47-a3e9-40ba-8341-ebb2016fe5f8

          // 然后就可以创建a标签 最后下载下来了
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', 'CEB621Message.xml')
          document.body.appendChild(link)
          link.click()
        }
      })
    },
    /** 查询回执 */
    select(row) {
      const that = this
      const postIds = row ? row.orderId : that.ids.map(item => item.orderId).toString()
      const orderId = row ? [{ orderId: row.orderId, copNo: row.copNo }] : that.ids
      that.$confirm('是否同步' + postIds + '等清单回执?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        orderClearSelect(orderId).then(response => {
          // console.log(response)
          if (response.code === 10000) {
            if (row) {
              that.dialogVisible = true
              that.info = response.data
            } else {
              that.$message.success(response.message)
            }
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        orderId: undefined,
        ConsigneeCode: undefined,
        buyerName: undefined,
        // 日期范围
        orderDate: [],
        pushInventory: undefined
      }
      this.handleQuery()
    },
    /** 导出订单列表 */
    exportOrder() {
      const that = this
      that.loading = true
      let params = {}
      // exportType 1:选中导出 2：列表导出
      if (that.ids.length > 0) {
        params.ids = that.ids.toString()
      } else {
        params = that.queryParams
      }

      exportClearOrder(params).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          window.open(response.data.url)
        } else {
          that.$message.error(response.message)
        }
        that.loading = false
      })
    },
    /** 导出订单商品列表 */
    exportOrderGoods() {
      const that = this
      this.loading = true
      let params = {}
      // exportType 1:选中导出 2：列表导出
      if (that.ids.length > 0) {
        params.ids = that.ids.toString()
        params.exportType = 1
      } else {
        params = that.queryParams
        params.exportType = 2
      }
      exportClearOrderGoods(params).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          window.open(response.data.url)
        } else {
          that.$message.error(response.message)
        }
        that.loading = false
      })
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.startTime = value[0]
        this.queryParams.endTime = value[1]
      }
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = []
      selection.map(item => {
        const info = {
          copNo: item.copNo,
          orderId: item.orderId
        }
        this.ids.push(info)
      })
      this.multiple = !selection.length
    }
  }
}
</script>
